package com.boc.langxiang.ui.activity.index;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.index.HotArticleContract;
import com.boc.langxiang.entity.request.HotArticleRequest;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.presenter.index.HotArticlePresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.adapter.index.HotArticleAdapter;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门文章
 */
public class HotArticleActivity extends BaseToolBarActivity implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener, HotArticleContract.View {

    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    private HotArticleAdapter mArticleAdapter;
    private HotArticleContract.Presenter mPresenter;
    private boolean mIsOnRefresh;
    private boolean mIsLoadmore;
    private int mCurrentPage = 1;
    private HotArticleRequest mHotArticleRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_article);
        ButterKnife.bind(this);
        setToolBarTitle("热门文章");
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new LinearLayoutManager(mContext));

        initAdapter();
        initData();
    }

    private void initData() {
        if (mHotArticleRequest == null) {
            mHotArticleRequest = new HotArticleRequest();
            mPresenter = new HotArticlePresenter(this);
        }
        mHotArticleRequest.page = mCurrentPage;
        mPresenter.getSpcialList(mHotArticleRequest);
    }


    private void initAdapter() {
        ArrayList<ArticleEntity> articles = new ArrayList<>();
        mArticleAdapter = new HotArticleAdapter(mContext, articles);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mArticleAdapter.setEmptyView(emptyView);
        mArticleAdapter.openLoadAnimation();
        //控制itemview动画是否只在第一次显示
        mArticleAdapter.isFirstOnly(false);
        mArticleAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mArticleAdapter.setOnLoadMoreListener(this);
        mRecylerview.setAdapter(mArticleAdapter);
        mArticleAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                ArticleEntity item = mArticleAdapter.getItem(position);
                intent.putExtra("data", item);
                startActivity(intent);
            }
        });
    }


    @Override
    protected boolean isSetTypeFace() {
        return true;
    }

    @Override
    public void onRefresh() {
        if (mIsLoadmore || mIsOnRefresh) {
            mSwipeLayout.setRefreshing(false);
            return;
        }
        mArticleAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mIsOnRefresh = true;
        mCurrentPage = 1;
        initData();
    }

    @Override
    public void onLoadMoreRequested() {
        mRecylerview.post(new Runnable() {
            @Override
            public void run() {
                if (mIsLoadmore || mIsOnRefresh) {
                    return;
                }
                mIsLoadmore = true;
                mCurrentPage++;
                initData();
            }
        });
    }

    @Override
    public void showLoading() {
        if (mIsOnRefresh) {
            return;
        }
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    @Override
    public void getFlagSuccess(ArrayList<ArticleEntity> getFlaglist) {
        if (mIsLoadmore) {
            mIsLoadmore = false;
            if (getFlaglist == null) {
                loadDone();
                mArticleAdapter.notifyDataChangedAfterLoadMore(false);
                return;
            }
            if (getFlaglist.size() < Constant.PAGE_SIZE) {
                loadDone();
                mArticleAdapter.notifyDataChangedAfterLoadMore(getFlaglist, false);
            } else {
                mArticleAdapter.notifyDataChangedAfterLoadMore(getFlaglist, true);
            }

        } else {
            mIsOnRefresh = false;
            if (getFlaglist == null) {
                return;
            }
            mArticleAdapter.setNewData(getFlaglist);
        }
    }

    private void loadDone() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) mRecylerview.getParent(), false);
        mArticleAdapter.addFooterView(view);
    }

}
